package com.party.core.service.commune.impl;


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.party.common.utils.StringUtils;
import com.party.core.api.member.IMemberActService;
import com.party.core.exception.BusinessException;
import com.party.core.model.commune.CIntegralItem;
import com.party.core.model.commune.CIntegralType;
import com.party.core.model.commune.CUserIntegral;
import com.party.core.model.commune.IntegralType;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.member.PartnerMember;
import com.party.core.service.commune.ICIntegralBizService;
import com.party.core.service.commune.ICIntegralItemService;
import com.party.core.service.commune.ICIntegralTypeService;
import com.party.core.service.commune.ICUserIntegralService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IPartnerMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @program: party
 * @description: 积分业务接口实现
 * @author: Hua Qunhao
 * @create: 2019-08-26 17:18
 **/
@Service
public class CIntegralBizService implements ICIntegralBizService {

    @Autowired
    private ICUserIntegralService cUserIntegralService;
    @Autowired
    private IMemberActService memberActService;
    @Autowired
    private IPartnerMemberService partnerMemberService;
    @Autowired
    private ICIntegralTypeService integralTypeService;
    @Autowired
    private ICIntegralItemService integralItemService;
    @Autowired
    private IMemberGroupService memberGroupService;

    /**
     * @Description:给活动报名者批量增加积分
     * @Param: [cUserIntegral, actPublisher]
     * @return: void
     * @Author: Hua Qunhao
     * @Date: 2019/8/26
     */
    @Override
    public List<String> batchAddIntegralAct(CUserIntegral cUserIntegral, String actPublisher) {
        String partnerId = memberGroupService.getPartnerId(actPublisher);
        HashMap<String, Object> params = Maps.newHashMap();
        params.put("actId", cUserIntegral.getActivityId());
        if (1 == cUserIntegral.getMemberType()) {
            params.put("signin", cUserIntegral.getMemberType());
        }
        List<Map<String, Object>> memberActs = memberActService.memberActlist2(params);
        List<String> members = Lists.newArrayList();
        for (Map<String, Object> mmp : memberActs) {
            String memberId = (String) mmp.get("member");
//            PartnerMember byPartnerAndMember = partnerMemberService.findByPartnerAndMember(actPublisher, (String) mmp.get("member"));
            CUserIntegral cOrg = new CUserIntegral();
            cOrg.setIntegralItemId(cUserIntegral.getIntegralItemId());
//            cOrg.setPmId(byPartnerAndMember.getId());
            cOrg.setIntegralTypeId(cUserIntegral.getIntegralTypeId());
            cOrg.setPartnerId(partnerId);
            cOrg.setMemberId(memberId);
            members.add(memberId);
            cUserIntegralService.insert(cOrg);
        }
        return members;
    }

    /**
     * @Description:获取机构的积分项
     * @Param:partnerId
     * @return:CIntegralItem
     * @Author: Hua Qunhao
     * @Date: 2019/10/30
     */
    public CIntegralItem getSystemCIntegralItem(String partnerId, String integralItemId, Integer integralType) throws Exception {

        CIntegralItem itemOrg = null;
        if (StringUtils.isNotEmpty(integralItemId)) {
            itemOrg = integralItemService.get(integralItemId);
        } else {
            CIntegralType cIntegralType = this.getCIntegralType(partnerId, integralType);
            if (cIntegralType == null) {
                throw new BusinessException("没有找到" + IntegralType.getValue(integralType) + "的积分类型");
            }
            CIntegralItem cIntegralItem = new CIntegralItem();
            cIntegralItem.setTypeId(cIntegralType.getId());

            List<CIntegralItem> integralItems = new ArrayList<>();
            if (IntegralType.CINTEGRALTYPE_CATEGORY_RECOMMEND.getCode() == integralType) {
                cIntegralItem.setRecommendGrade(CIntegralItem.CINTEGRALITEM_GRADE_ORDINARY);
                integralItems = integralItemService.list(cIntegralItem);
            } else if (IntegralType.CINTEGRALTYPE_CATEGORY_GIFT.getCode() == integralType) {
                cIntegralItem.setGiftGrade(CIntegralItem.CINTEGRALITEM_GRADE_ORDINARY);
                integralItems = integralItemService.list(cIntegralItem);
            }
            if (integralItems.size() == 0) {
                throw new BusinessException("没有找到" + IntegralType.getValue(integralType) + "的积分项");
            }
            itemOrg = integralItems.get(0);
        }
        return itemOrg;
    }

    /**
     * @Description:插入积分
     * @Param:a
     * @return:a
     * @Author: Hua Qunhao
     * @Date: 2019/10/31
     */
    public void insertSystemCIntegralItem(String cmrId, String partnerId, PartnerMember partnerMember, String integralItemId, Integer integralType) throws Exception {
        String newPartnerId = memberGroupService.getPartnerId(partnerMember.getPartnerId());
        //获取推荐积分类型
        CIntegralItem cIntegralItem = this.getSystemCIntegralItem(partnerId, integralItemId, integralType);

        //添加积分
        CUserIntegral cUserIntegral = new CUserIntegral();
        cUserIntegral.setIntegralTypeId(cIntegralItem.getTypeId());
        cUserIntegral.setPartnerId(newPartnerId);
        cUserIntegral.setMemberId(partnerMember.getMemberId());
        cUserIntegral.setRecommendId(cmrId);
        cUserIntegral.setIntegralItemId(cIntegralItem.getId());

        if(IntegralType.CINTEGRALTYPE_CATEGORY_GIFT.getCode()==integralType){
            //指定添加时间
            cUserIntegral.setCreateDate(partnerMember.getCreateDate());
            cUserIntegral.setUpdateDate(partnerMember.getCreateDate());
        }
        cUserIntegralService.insert(cUserIntegral);
    }

    /**
     * @Description:获取积分类型
     * @Param:a
     * @return:a
     * @Author: Hua Qunhao
     * @Date: 2019/10/31
     */
    public CIntegralType getCIntegralType(String partnerId, Integer category) {
        CIntegralType cIntegralType = new CIntegralType();
        cIntegralType.setCreateBy(partnerId);
        cIntegralType.setCategory(category);
        List<CIntegralType> list = integralTypeService.list(cIntegralType);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }


    /**
     * @Description: 添加赠送积分
     * @Param:partnerId，memberId
     * @return:
     * @Author: Hua Qunhao
     * @Date: 2019/11/25
     */
    public void addGiftIntegral(String partnerId, String memberId) throws Exception {
        PartnerMember mainOrg = partnerMemberService.getMainOrg(memberId, partnerId);
        MemberGroup partner = memberGroupService.getPartner(partnerId);
        CIntegralItem cIntegralItem = new CIntegralItem();
        cIntegralItem.setGiftGrade(CIntegralItem.CINTEGRALITEM_GRADE_ORDINARY);
        cIntegralItem.setCreateBy(partner.getId());
        CIntegralItem ciiOrg = null;
        List<CIntegralItem> list = integralItemService.list(cIntegralItem);
        if (list.size() == 0) {
            throw new BusinessException("添加赠送积分失败,没有找到的积分项");
        } else {
            ciiOrg = list.get(0);
            CUserIntegral cUserIntegral = new CUserIntegral();
            cUserIntegral.setIntegralItemId(ciiOrg.getId());
            cUserIntegral.setPartnerId(partner.getId());
            cUserIntegral.setMemberId(memberId);
            List<CUserIntegral> list1 = cUserIntegralService.list(cUserIntegral);
            if (list1.size() == 0) {
                this.insertSystemCIntegralItem(null, partner.getId(), mainOrg, null, IntegralType.CINTEGRALTYPE_CATEGORY_GIFT.getCode());
            }
        }
    }
}
